package com.swd.order.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.swd.order.mapper.CategoryMapper;
import com.swd.order.model.Category;
import com.swd.order.model.CategoryExample;
import com.swd.order.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> all(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("`index`");
        return categoryMapper.selectByExample(categoryExample);
    }

    public List<CategoryVo> queryAll() {


        return categoryMapper.queryAll();
    }

    public Page<CategoryVo> queryAllPageAble(int pageNum,int limit) {
        Page<CategoryVo> page = PageHelper.startPage(pageNum,limit);
        categoryMapper.queryAll();
        return page;
    }

    public Category queryById(int id){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(id);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        return categories.size()>0?categories.get(0):null;
    }

    public List<Category> queryByName(String name){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andCategoryNameEqualTo(name);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        return categories;
    }


    public boolean update(Category category) {

        int i = categoryMapper.updateByPrimaryKeySelective(category);

        return i == 1;
    }

    public boolean save(Category category) {

        int i = categoryMapper.insertSelective(category);

        return i == 1;
    }

    public boolean delete(int id) {
        int i = categoryMapper.deleteByPrimaryKey(id);
        return i == 1;
    }
}
